package com.zjw.dao.impl;

import com.zjw.dao.BaseDao;
import com.zjw.entity.User;
import com.zjw.dao.UserDao;
import com.zjw.utils.ConnectionUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 普通用户信息表(User)表数据库访问层实现类
 *
 * @author 翟
 * @since 2025-04-09 09:21:58
 */
public class UserDaoImpl extends BaseDao implements UserDao {

    public UserDaoImpl(Connection connection) {
        super(connection);
    }

    @Override
    public User selectUserByUsernameAndPassword(String username, String password) throws SQLException {
        String sql = "select user_id , user_name , password , balance from user where user_name = ? and password = ?";
        ResultSet resultSet = null;
        try {
            resultSet = executeQuery(sql , username , password);
            if (resultSet.next()) {
                User user = new User();
                user.setUserId(resultSet.getInt("user_id"));
                user.setUserName(resultSet.getString("user_name"));
                user.setPassword(resultSet.getString("password"));
                user.setBalance(resultSet.getDouble("balance"));
                return user;
            }
        } finally {
            ConnectionUtil.closeALL(resultSet , null , null);
        }
        return null;
    }

    @Override
    public int updateBalanceByUserId(int userId , double price , char AdditionOrSubtractionSymbols) throws SQLException {
        String sql = "update user set balance = balance " + AdditionOrSubtractionSymbols + " ? where user_id = ? ";
        return executeUpdate(sql , price , userId);
    }
}
